﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TalksLog.Domain.Account.Entities;
using TalksLog.Domain.Account.Repositores;
using TalksLog.Domain.Core;

namespace TalksLog.Infrastructure.EFRepository
{
    public class AccountRepository : IAccountRepository
    {
        private EFDbContext _context;

        public AccountRepository(RepositoryDataContextBase context)
        {
            _context = (context as RepositoryEFDataContext).DBContext;
        }

        public void Add(Account entity)
        {
            _context.Accounts.Add(entity);
        }

        public Account Find(string id)
        {
            Account user = _context.Accounts.Find(id);
            return user;
        }

        public void Remove(Account entity)
        {
            _context.Entry<Account>(entity).State = System.Data.Entity.EntityState.Deleted;
        }

        public void Update(Account entity)
        {
            _context.Entry<Account>(entity).State = System.Data.Entity.EntityState.Modified;
        }

        public bool DoesEmailExist(string email)
        {
            return _context.Accounts.Any(c => c.Email == email);
        }

        public bool DoesNameExist(string name)
        {
            return _context.Accounts.Any(c => c.Name == name);
        }

        public Account FindByEmail(string email)
        {
            return _context.Accounts.Where(c => c.Email == email).FirstOrDefault();
        }
    }
}
